<html>
<head><meta charset="utf-8"><title>substitutions question · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html">substitutions question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="136416313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136416313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136416313">(Oct 24 2018 at 15:16)</a>:</h4>
<p>Example: f&lt;T:Trait&gt;(a:T) {a.m()}.  If I have a substitution T → A for f, and substitution Self → T (rustc::ty::subst::Substs) for the call to Trait::a from f. How can I combine the two substitutions to end up with a.m substitution (Self → A)?</p>



<a name="136417632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136417632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136417632">(Oct 24 2018 at 15:37)</a>:</h4>
<p>I <em>think</em> you want <code>Substs::rebase_onto()</code> but I'm not sure <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/subst/type.Substs.html#method.rebase_onto" target="_blank" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/subst/type.Substs.html#method.rebase_onto">https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/subst/type.Substs.html#method.rebase_onto</a></p>



<a name="136418983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136418983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136418983">(Oct 24 2018 at 15:58)</a>:</h4>
<p>I thought about that too, but I can't figure out what to put in source_ancestor and target_substs.</p>



<a name="136419213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136419213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136419213">(Oct 24 2018 at 16:02)</a>:</h4>
<p><span class="user-mention" data-user-id="120905">@Nora</span> I'm a bit confused by your question</p>



<a name="136419222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136419222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136419222">(Oct 24 2018 at 16:02)</a>:</h4>
<p><code>rebase_onto</code> is used when you have the substitutions from a trait call and you know the substitutions from the specific impl that is implementing the trait</p>



<a name="136419226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136419226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136419226">(Oct 24 2018 at 16:02)</a>:</h4>
<p>is that the situation you have?</p>



<a name="136419542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136419542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136419542">(Oct 24 2018 at 16:07)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  I don't think that is my situation, I have the substitutions of f from its caller, and I have the substitutions of callee m inside f. <br>
I want to get the substitutions of m in the context of the f with the given substitutions from its caller. Does this make sense?</p>



<a name="136420359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420359">(Oct 24 2018 at 16:21)</a>:</h4>
<p>that is much easier</p>



<a name="136420400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420400">(Oct 24 2018 at 16:22)</a>:</h4>
<p>you just need to apply one set of substitutions to the other</p>



<a name="136420448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420448">(Oct 24 2018 at 16:23)</a>:</h4>
<p>How?</p>



<a name="136420469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420469">(Oct 24 2018 at 16:23)</a>:</h4>
<p><span class="user-mention" data-user-id="120905">@Nora</span> e.g.,</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">callee</span><span class="p">,</span><span class="w"> </span><span class="n">caller_subst</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">callee_subst</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_value</span><span class="p">(</span><span class="n">callee</span><span class="p">);</span><span class="w"></span>

<span class="c1">// now we have the type of `value` expressed in terms of its *own* generic parameters</span>
<span class="kd">let</span><span class="w"> </span><span class="n">value_ty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">type_of</span><span class="p">(</span><span class="n">value</span><span class="p">);</span><span class="w"></span>

<span class="c1">// now we have the type of `value` expressed in terms of the *callee&#39;s* generic parameters</span>
<span class="kd">let</span><span class="w"> </span><span class="n">value_ty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value_ty</span><span class="p">.</span><span class="n">subst</span><span class="p">(</span><span class="n">callee_subst</span><span class="p">);</span><span class="w"></span>

<span class="c1">// now we have it in terms of the *caller&#39;s* generic parameters</span>
<span class="kd">let</span><span class="w"> </span><span class="n">value_ty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value_ty</span><span class="p">.</span><span class="n">subst</span><span class="p">(</span><span class="n">caller_subst</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="136420479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420479">(Oct 24 2018 at 16:24)</a>:</h4>
<p>or you could compose the substitutions first</p>



<a name="136420553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420553">(Oct 24 2018 at 16:24)</a>:</h4>
<p>e.g.,</p>
<div class="codehilite"><pre><span></span><span class="c1">// callee_subst was (ValueType -&gt; CalleeType)</span>
<span class="c1">// caller_subst is (CalleeType -&gt; CallerType)</span>
<span class="c1">// composed_subst is (ValueType -&gt; CallerType)</span>
<span class="kd">let</span><span class="w"> </span><span class="n">composed_subst</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">callee_subst</span><span class="p">.</span><span class="n">subst</span><span class="p">(</span><span class="n">caller_subst</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="136420560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420560">(Oct 24 2018 at 16:25)</a>:</h4>
<p>hopefully I got that order right :)</p>



<a name="136420566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420566">(Oct 24 2018 at 16:25)</a>:</h4>
<p>I find the first easier to see</p>



<a name="136420999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/136420999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#136420999">(Oct 24 2018 at 16:32)</a>:</h4>
<p>Thank you! The second one works.</p>



<a name="137168533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/137168533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#137168533">(Nov 04 2018 at 19:43)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  I need a little more help with the substitution. I have the def_id if a method implementation, and I need to get all substitutions coming from the impl. <br>
Consider the code:<br>
pub trait RngCore {<br>
    fn next_u32(&amp;mut self);<br>
}<br>
pub trait BlockRngCore {<br>
    type Item;<br>
    fn generate(&amp;mut self);<br>
}<br>
pub struct BlockRng&lt;R: BlockRngCore&gt; {<br>
    pub core: R,<br>
}</p>
<p>impl&lt;R: BlockRngCore&gt; BlockRng&lt;R&gt; {<br>
    pub fn generate_and_set(&amp;mut self) {<br>
        self.core.generate();<br>
    }<br>
}</p>
<p>impl&lt;R: BlockRngCore&lt;Item=u32&gt;&gt; RngCore for BlockRng&lt;R&gt;<br>
{<br>
    fn next_u32(&amp;mut self)  {<br>
        self.generate_and_set();<br>
    }<br>
}</p>
<p>I have the def_id of next_u32, I need to find somehow the substition R--&gt; BlockRngCore&lt;Item=u32&gt;, so I can propagate it. I looked around for a while and I can't figure it out.</p>



<a name="137172564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/137172564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#137172564">(Nov 04 2018 at 21:49)</a>:</h4>
<p>you can use markdown to paste code (wrap it in 3 backticks)</p>



<a name="146787852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/substitutions%20question/near/146787852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nora <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/substitutions.20question.html#146787852">(Nov 05 2018 at 10:40)</a>:</h4>
<p>Here is the code properly displayed:</p>
<div class="codehilite"><pre><span></span>pub trait RngCore {
    fn next_u32(&amp;mut self);
}
pub trait BlockRngCore {
    type Item;
    fn generate(&amp;mut self);
}
pub struct BlockRng&lt;R: BlockRngCore&gt; {
    pub core: R,
}

impl&lt;R: BlockRngCore&gt; BlockRng&lt;R&gt; {
    pub fn generate_and_set(&amp;mut self) {
        self.core.generate();
    }
}

impl&lt;R: BlockRngCore&lt;Item=u32&gt;&gt; RngCore for BlockRng&lt;R&gt;
{
    fn next_u32(&amp;mut self)  {
        self.generate_and_set();
    }
}
</pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>